<?php 
namespace App\Http\Controllers\TongGaoApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Mysql\tonggao\article;
use App\Models\Mysql\tonggao\term;
use App\Models\Mysql\tonggao\term_detail;
use App\Models\Mysql\tonggao\term_detail_log;
use App\Models\Mysql\tonggao\sentence;
use App\Models\Mysql\tonggao\sentence_log;

class TongGaoController extends Controller
{
//	use GlobalData;

	const ROUTE_PREFIX = '/api/*';

	protected $request;

	protected $curTime;

	protected $debug;


	/**
	 * 创建新的`DictController`控制器实例
	 * @param \Illuminate\Http\Request $request
	 */
	public function __construct(Request $request)
	{
		$this->request = $request;
		$this->error = [];
		$this->curTime = time();

		if(config('app.debug')) {
			$this->debug = true;
			error_reporting(E_ALL | E_STRICT);
		} else {
			$this->debug = false;
		}
	}

	/**
	 * 1. 获取所有术语列表接口
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listTermsByPage()
	{
//		$page = ( !$this->request->page ) ? 1 : intval($this->request->page);
		$size = ( !$this->request->size ) ? 10 : intval($this->request->size);
		$pattern = ( $this->request->pattern ) ? addslashes( $this->request->pattern ) : "";
		
		$result = term::getDataByPage($size,$pattern);
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
	}

	/**
	 * 2. 查询包含某个术语的所有文稿
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listArticlesByTerm()
	{
		$size = ( !$this->request->size ) ? 10 : intval($this->request->size);

		$result = article::getDataByTermIDByPage(intval($this->request->term_id), $size);
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
	}

	/**
	 * 3. 给定某个术语和某个文稿，查询术语详情，
	 * 包括：这个术语在这个文稿不同位置的翻译、句对（中文和英文句子）、是否在统稿平台修改过。
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listTermDetailByTermArticleByPage()
	{
		$size = ( !$this->request->size ) ? 10 : intval($this->request->size);

		$result = term_detail::getDataByTermIdArticleIdByPage(
			intval($this->request->term_id),
			intval($this->request->article_id),
			$size
		);
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
	}

	/**
	 * 4. 查询某个term_detail对应的修改日志.
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listLogsByTermDetailByPage()
	{
		$size = ( !$this->request->size ) ? 10 : intval($this->request->size);

		$result = term_detail_log::getLogsByTermDetailByPage(
			intval($this->request->term_detail_id),
			$size
		);
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
	}

	/**
	 * 5. 查询某个sentence对应的修改日志.
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listSentenceLogsByPage()
	{
		$size = ( !$this->request->size ) ? 10 : intval($this->request->size);

		$result = sentence_log::getSentenceLogsByPage(
			intval($this->request->sentence_id),
			$size
		);
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
	}

	/**
	 * 6. 查询某个术语的每一种译法使用的次数
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listCountPerTranslationOfTerm()
	{
		$term_id = intval($this->request->term_id);
		$criteria = ($this->request->criteria) ? addslashes($this->request->criteria) : "";
		
		$articleSql = "article_id<>56";
		if ($criteria=="lamrim"){
			$articleSql = "article_id=56";
		}
		
		$result = term_detail::select(
			"select english,count(*) count from term_detail where term_id=$term_id and $articleSql and length(english)>0 and english is not null group by english order by count desc"
		);
		return responseJson('ok', $result, TRUE);
		/*
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
		*/
	}
	
	/**
	 * 7. 查询某个术语的参考信息
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listTermInfo()
	{
		$term_id = intval($this->request->term_id);

		$result = term::select(
			"select source,info from term_info where term_id=$term_id"
		);
		foreach($result as $k=>$v)
			$v->info = str_replace("[dd]","\n",$v->info);
			
		return responseJson('ok', $result, TRUE);
		/*
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
		*/
	}
	
	/**
	 * 8. 根据术语的中文查询术语的参考信息
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listTermInfoByChineseKey()
	{
		$chinese_key = addslashes($this->request->chinese_key);

		$fieldNameArray = array("d_seq_no","d_chinese_key","d_freq_grade","d_proposal","d_adopted","d_comment","d_lamrim","d_dic_english","d_ddb","d_current_article","d_sanskrit","d_pali","d_foguang2","d_dic_chinese","d_article_456");
		$titleArray = array("序号","中文","频率分级","初次建议","采信","注","广论","辞典（英）","ddb","文集","梵语","巴利语","佛光大辞典","辞典（中）","文集4-6册");
		
		$result = term::select(
			"select * from term where chinese_key=\"$chinese_key\""
		);
		if (!empty($result)){
			$d_flag=$result[0]->d_flag;
			if ($d_flag==0){
				$result = term::select(
					"select term_info.source,term_info.info from term,term_info where term.id=term_info.term_id and chinese_key=\"$chinese_key\""
				);
				foreach($result as $k=>$v)
					$v->info = str_replace("[dd]","\n",$v->info);
			}else{
				$rr=array();
				$rrCount=0;
				foreach($fieldNameArray as $i=>$fieldName){
					if ($fieldName=="d_chinese_key")
						continue;
					if (empty($result[0]->{$fieldName}))
						continue;
					$rr[$rrCount]['source']=$titleArray[$i];
					$rr[$rrCount]['info']=$result[0]->{$fieldName};
					$rr[$rrCount]['info'] = str_replace("[dd]"," // ",$rr[$rrCount]['info']);
					$rr[$rrCount]['info'] = str_replace("[b]"," // ",$rr[$rrCount]['info']);
					$rr[$rrCount]['info'] = str_replace("\n"," // ",$rr[$rrCount]['info']);
					$rrCount++;
				}
				$result=$rr;
			}
		}	

		return responseJson('ok', $result, TRUE);
		/*
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
		*/
	}
	
	/**
	 * 9. 获取术语列表接口-2
	 *    不统计count
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function listTermsByPage2()
	{
		$chinese_key_part = addslashes($this->request->chinese_key_part);
		$result = term::select("select id,chinese_key value from term where chinese_key like \"%$chinese_key_part%\" and batch>0 order by batch desc, chinese_key");
		if( !is_null($result) )
			return responseJson('ok', $result, TRUE);
		else
			return responseJson('failed');
	}
	
    public function test()
    {
           
    }

} //!- TongGaoController
?>
